import { toast } from 'sonner';

// 全局确认对话框状态管理
let confirmDialogCallback: ((confirmed: boolean) => void) | null = null;
let confirmDialogState = {
  isOpen: false,
  title: '',
  message: '',
  type: 'info' as 'danger' | 'warning' | 'info' | 'success',
  confirmText: '确认',
  cancelText: '取消'
};

// 状态变化监听器
let stateChangeListeners: (() => void)[] = [];

// 通知状态变化
const notifyStateChange = () => {
  console.log('notifyStateChange 被调用，监听器数量:', stateChangeListeners.length); // 调试信息
  stateChangeListeners.forEach((listener, index) => {
    console.log(`调用监听器 ${index}`); // 调试信息
    listener();
  });
};

// 添加状态变化监听器
export const addStateChangeListener = (listener: () => void) => {
  stateChangeListeners.push(listener);
  return () => {
    stateChangeListeners = stateChangeListeners.filter(l => l !== listener);
  };
};

// 设置确认对话框状态
export const setConfirmDialog = (config: {
  title: string;
  message: string;
  type?: 'danger' | 'warning' | 'info' | 'success';
  confirmText?: string;
  cancelText?: string;
}) => {
  // 创建新的状态对象，确保 React 能检测到变化
  confirmDialogState = {
    isOpen: true,
    ...config,
    type: config.type || 'info',
    confirmText: config.confirmText || '确认',
    cancelText: config.cancelText || '取消'
  };
  notifyStateChange();
};

// 关闭确认对话框
export const closeConfirmDialog = () => {
  console.log('closeConfirmDialog 开始执行'); // 调试信息
  // 创建新的状态对象，确保 React 能检测到变化
  confirmDialogState = {
    ...confirmDialogState,
    isOpen: false
  };
  if (confirmDialogCallback) {
    console.log('closeConfirmDialog 调用回调函数(false)'); // 调试信息
    confirmDialogCallback(false);
    confirmDialogCallback = null;
  } else {
    console.log('closeConfirmDialog 回调函数为 null'); // 调试信息
  }
  console.log('closeConfirmDialog 设置状态后，调用 notifyStateChange'); // 调试信息
  notifyStateChange();
  console.log('closeConfirmDialog 执行完成'); // 调试信息
};

// 确认操作
export const confirmAction = () => {
  console.log('confirmAction 开始执行'); // 调试信息
  if (confirmDialogCallback) {
    console.log('confirmAction 调用回调函数(true)'); // 调试信息
    confirmDialogCallback(true);
    confirmDialogCallback = null;
  } else {
    console.log('confirmAction 回调函数为 null'); // 调试信息
  }
  console.log('confirmAction 关闭对话框'); // 调试信息
  closeConfirmDialog();
};

// 获取确认对话框状态
export const getConfirmDialogState = () => ({ ...confirmDialogState });

// 统一的确认对话框函数
export const showConfirm = (
  title: string,
  message: string,
  options?: {
    type?: 'danger' | 'warning' | 'info' | 'success';
    confirmText?: string;
    cancelText?: string;
  }
): Promise<boolean> => {
  return new Promise((resolve) => {
    confirmDialogCallback = resolve;
    setConfirmDialog({
      title,
      message,
      type: options?.type || 'info',
      confirmText: options?.confirmText || '确认',
      cancelText: options?.cancelText || '取消'
    });
  });
};

// 便捷的确认函数
export const confirm = {
  // 危险操作确认
  danger: (title: string, message: string, confirmText = '删除', cancelText = '取消') => 
    showConfirm(title, message, { type: 'danger', confirmText, cancelText }),
  
  // 警告操作确认
  warning: (title: string, message: string, confirmText = '确认', cancelText = '取消') => 
    showConfirm(title, message, { type: 'warning', confirmText, cancelText }),
  
  // 信息确认
  info: (title: string, message: string, confirmText = '确认', cancelText = '取消') => 
    showConfirm(title, message, { type: 'info', confirmText, cancelText }),
  
  // 成功确认
  success: (title: string, message: string, confirmText = '确认', cancelText = '取消') => 
    showConfirm(title, message, { type: 'success', confirmText, cancelText })
};

// 统一的提示函数
export const showMessage = {
  // 成功提示
  success: (message: string, options?: any) => toast.success(message, options),
  
  // 错误提示
  error: (message: string, options?: any) => toast.error(message, options),
  
  // 信息提示
  info: (message: string, options?: any) => toast.info(message, options),
  
  // 警告提示
  warning: (message: string, options?: any) => toast.warning(message, options)
}; 